package org.Image.management.SpringDataService.config;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.Image.management.SpringDataService.Pojo.AppConfig;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@Configuration
@RequiredArgsConstructor
@Slf4j
public class initFileConfig {
    // PostConstruct 修饰的方法会在服务器加载Servlet的时候运行，并且只会被服务器调用一次
    // PreDestroy修饰的方法会在服务器卸载Servlet的时候运行
    private final AppConfig appConfig;
    // 初始化上传目录和配置文件
    @PostConstruct
    public void initCreateDirectories() throws IOException {
        // 创建上传目录（如果不存在）
        Path uploadPath = Path.of(new File(appConfig.getUploadPath()).getAbsolutePath());
        if (!Files.exists(uploadPath)) {
            Files.createDirectories(uploadPath);
        }
        log.info("初始化上传目录完成,文件初始位置：{}",uploadPath.toAbsolutePath());
    }
}
